package com
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	public class CharacterData extends FlaObject
	{
		public var _name:flaInputText;
		public var _trueName:flaInputText;
		public var _race:RaceInput;
		public var _class:ClassInput;
		public var _level:FlaText;
		public var _hp:FlaText;
		public var _deaths:flaInputText;
		public var _pp:flaInputText;
		public var _blankets:flaInputText;
		public var _totalCP:FlaText;
		public var _spentCP:FlaText;
		public var _freeCP:FlaText;
		
		private var _titleName:FlaText = new FlaText("Character Name:");
		private var _titleTrue:FlaText = new FlaText("True Name:");
		private var _titleRace:FlaText = new FlaText("Race:");
		private var _titleClass:FlaText = new FlaText("Class:");
		private var _titleLevel:FlaText = new FlaText("Level:");
		private var _titleHP:FlaText = new FlaText("Hit Points:");
		private var _titleDeaths:FlaText = new FlaText("Deaths:");
		private var _titlePP:FlaText = new FlaText("Production\nPoints:");
		private var _titleBlankets:FlaText = new FlaText("Blankets\nApplied:");
		private var _titleTotal:FlaText = new FlaText("Total CP:");
		private var _titleSpent:FlaText = new FlaText("Spent CP:");
		private var _titleFree:FlaText = new FlaText("Free CP:");
		
		private var _theSkills:TheSkillList;
		public var _characterSkills:SkillList;
		
		public function CharacterData()
		{
			addChild(_titleName);
			addChild(_titleTrue);
			addChild(_titleRace);
			addChild(_titleClass);
			addChild(_titleLevel);
			addChild(_titleHP);
			addChild(_titleDeaths);
			addChild(_titlePP);
			addChild(_titleBlankets);
			addChild(_titleTotal);
			addChild(_titleSpent);
			addChild(_titleFree);
			
			_name = new flaInputText();
			addChild(_name);
			_trueName = new flaInputText();
			addChild(_trueName);
			_race = new RaceInput();
			addChild(_race);
			_class = new ClassInput();
			addChild(_class);
			_level = new FlaText();
			addChild(_level);
			_hp = new FlaText();
			addChild(_hp);
			_deaths = new flaInputText();
			addChild(_deaths);
			_pp = new flaInputText();
			addChild(_pp);
			_blankets = new flaInputText();
			addChild(_blankets);
			_totalCP = new FlaText();
			addChild(_totalCP);
			_spentCP = new FlaText();
			addChild(_spentCP);
			_freeCP = new FlaText();
			addChild(_freeCP);
			
			_name.border = true;
			_name.tabIndex = 4;
			_name.width = 200;
			_trueName.border = true;
			_trueName.tabIndex = 5;
			_trueName.width = 200;
			_race.border = true;
			_race.tabIndex = 6;
			_class.border = true;
			_class.tabIndex = 7;
			_level.border = true;
			_level.width = 50;
			_hp.border = true;
			_hp.width = 50;
			_deaths.border = true;
			_deaths.tabIndex = 10;
			_deaths.width = 50;
			_pp.border = true;
			_pp.tabIndex = 9;
			_pp.width = 50;
			_blankets.border = true;
			_blankets.tabIndex = 8;
			_blankets.width = 50;
			_totalCP.border = true;
			_totalCP.width = 50;
			_spentCP.border = true;
			_spentCP.width = 50;
			_freeCP.border = true;
			_freeCP.width = 50;
			
			_trueName.y = 50;
			_race.y = 100;
			_class.y = 150;
			_blankets.y = 220;
			_pp.y = 290;
			_deaths.y = 340;
			
			_level.x = 130;
			_level.y = 100;
			_hp.x = 130;
			_hp.y = 150;
			_totalCP.x = 130; 
			_totalCP.y = 200;
			_spentCP.x = 130; 
			_spentCP.y = 250;
			_freeCP.x = 130; 
			_freeCP.y = 300;
			
			_titleName.width = 200;
			_titlePP.width = 200;
			_titleBlankets.width = 200;
			_titleBlankets.height = 60;
			_titlePP.height = 60;
			
			_titleName.y = -20;
			_titleTrue.y = 30;
			_titleRace.y = 80;
			_titleClass.y = 130;
			_titleBlankets.y = 180;
			_titlePP.y = 250;
			_titleDeaths.y = 320;
			
			_titleLevel.x = 130;
			_titleLevel.y = 80;
			_titleHP.x = 130;
			_titleHP.y = 130;
			_titleTotal.x = 130;
			_titleTotal.y = 180;
			_titleSpent.x = 130;
			_titleSpent.y = 230;
			_titleFree.x = 130;
			_titleFree.y = 280;
			
			_blankets.addEventListener(Event.CHANGE, update);
			
			_theSkills = new TheSkillList();
			addChild(_theSkills);
			_theSkills.x = 650;
			_theSkills.addEventListener(MouseEvent.CLICK, onSkillAdd);
			
			_characterSkills = new SkillList();
			addChild(_characterSkills);
			_characterSkills.x = 300;
			_characterSkills.addEventListener(MouseEvent.CLICK, onSkillDel);
			
			_class.addEventListener(Event.CHANGE, update);
			
		}
		
		private function onSkillAdd(e:MouseEvent):void {
			if (e.target is Skill) {
				_characterSkills.addSkill(Skill(e.target));
				update();
			}
		}
		private function onSkillDel(e:MouseEvent):void {
			if (e.target is Skill) {
				_characterSkills.removeSkill(Skill(e.target));
				update();
			}
		}
		
		public function update(e:Event = null):void {
			onBlanketsChange();
			
			_theSkills.update(_class.text);
			_characterSkills.update(_class.text);
			
			if (_class.text.toLowerCase() == "mercenary" ||
				_class.text.toLowerCase() == "templar" ||
				_class.text.toLowerCase() == "ranger")
			{
				_hp.text = (Number(_level.text) * 2 + 4).toString();
			}else if (_class.text.toLowerCase() == "nightblade" ||
				_class.text.toLowerCase() == "assassin" ||
				_class.text.toLowerCase() == "witch hunter")
			{
				_hp.text = (Number(_level.text) * 1 + 3).toString();
			}else if (_class.text.toLowerCase() == "mage" ||
				_class.text.toLowerCase() == "druid" ||
				_class.text.toLowerCase() == "bard")
			{
				var hp:Number = 2;				
				var skip:Number = 0;
				for (var i:int = 0; i < Number(_level.text); i++) {
					if (skip == 2) {
						skip = 0;
					}else {
						skip++;
						hp++;
					}
				}
				_hp.text = hp.toString();
			}
			
			_spentCP.text = _characterSkills.spent.toString();
			_freeCP.text = (Number(_totalCP.text) - Number(_spentCP.text)).toString();
		}
		
		private function onBlanketsChange():void {			
			var cp:Number = 150;
			var level:Number = 1;
			for (var i:int = 0; i < Number(_blankets.text); i++) {
				switch(level) {
					case 0:
						cp += 65;
						if (cp >= 250)
							level++;
						break;
					case 1:
						cp += 65;
						if (cp >= 250)
							level++;
						break;
					case 2:
						cp += 43;
						if (cp >= 350)
							level++;
						break;
					case 3:
						cp += 34;
						if (cp >= 450)
							level++;
						break;
					case 4:
						cp += 28;
						if (cp >= 550)
							level++;
						break;
					case 5:
						cp += 24;
						if (cp >= 650)
							level++;
						break;
					case 6:
						cp += 22;
						if (cp >= 750)
							level++;
						break;
					case 7:
						cp += 19;
						if (cp >= 850)
							level++;
						break;
					case 8:
						cp += 17;
						if (cp >= 950)
							level++;
						break;
					case 9:
						cp += 16;
						if (cp >= 1050)
							level++;
						break;
					case 10:
						cp += 15;
						if (cp >= 1150)
							level++;
						break;
					case 11:
						cp += 14;
						if (cp >= 1250)
							level++;
						break;
					case 12:
						cp += 13;
						if (cp >= 1350)
							level++;
						break;
					case 13:
						cp += 12;
						if (cp >= 1450)
							level++;
						break;
					case 14:
						cp += 12;
						if (cp >= 1550)
							level++;
						break;
					case 15:
						cp += 11;
						if (cp >= 1650)
							level++;
						break;
					case 16:
						cp += 11;
						if (cp >= 1750)
							level++;
						break;
					default:
						cp += 10;
						level = Math.floor(cp / 100);
				}
			}
			_level.text = level.toString();
			if (_race.text.toLowerCase() == "human") {
				_totalCP.text = (cp + 50).toString();
			}else {
				_totalCP.text = cp.toString();
			}
		}
	}
}